fix: prevent Answer.from_dict methods from mutating their input dict - #11908
Conversation
…from mutating their input dict Both from_dict methods wrote parsed Document/ChatMessage/Span objects back into the caller's dictionary, so deserializing the same dict a second time received already-parsed objects and raised AttributeError. Document.from_dict already guards against this with data.copy(); this extends the same idea with deepcopy, since the mutated values are nested. Adds regression tests for both classes.
|
@vidigoat is attempting to deploy a commit to the deepset Team on Vercel. A member of the Team first needs to authorize it. |
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||
anakin87
left a comment
There was a problem hiding this comment.
Thank you!
I found an optimization opportunity.
|
Thanks, good call — One thing worth flagging: in |
Description
from_dictshould be side-effect free, but bothGeneratedAnswer.from_dictandExtractedAnswer.from_dictwrote parsedDocument/ChatMessage/Spanobjects back into the caller's dictionary. Deserializing the same dict a second time (common in replay/logging flows that keep the serialized form around) then crashed:Document.from_dictalready guards against this withdata = data.copy(); this extends the same idea usingdeepcopy, since the mutated values are nested insideinit_parameters/meta.Test plan
test_from_dict_does_not_mutate_inputto bothTestExtractedAnswerandTestGeneratedAnswer, each asserting the input dict is unchanged afterfrom_dictand that a second deserialization of the same dict still succeeds and is equal.main(input mutated / second call raises) and pass with this change.AI disclosure
This change was prepared with the help of an AI coding assistant; the bug was reproduced and the behavior verified before submitting.